/* Name : main.c * Purpose : Source code for Servo Motor Interfacing with Arduino. * Author : Gemicates * Date : 09-02-2018 * Website : www.gemicates.org * Revision : None */ #include<Servo.h> // header file for controlling servo Servo servo_sweep; // create servo object to control a servo int angle = 0; // variable to store the servo position void setup() { servo_sweep.attach(9); // attached the servo on pin 9 to the servo object } void loop() { for(int angle = 0; angle <=180; angle += 1) // goes from 0 degrees to 180 degrees in steps of 1 degree { servo_sweep.write(angle); // tell servo to go to position in variable 'angle' delay(20); // waits 20ms between servo commands } for(angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees { servo_sweep.write(angle); // move servo in opposite direction delay(20); // waits 20ms between servo commands } }